package com.esms.bean;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.esms.struts.service.EnterpriseService;

/**
 * A data access object (DAO) providing persistence and search support for
 * Employee entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.esms.bean.Employee
 * @author MyEclipse Persistence Tools
 */

public class EmployeeDAO extends BaseHibernateDAO {
	private EmployeeDAO() {}
	public static EmployeeDAO $ = new EmployeeDAO(); 
	
	private static final Log log = LogFactory.getLog(EmployeeDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String PERSONID = "personid";
	public static final String STATUS = "status";
	public static final String PROGRESS = "progress";

	public void save(Employee transientInstance) {
		log.debug("saving Employee instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	//added by Eric
	public void insert(Employee employee) {
	    Session session = HibernateSessionFactory.getSession();  
	    Transaction tx = session.beginTransaction();  
	    try {  
	        session.save(employee);  
	        tx.commit();  
	        log.info("Employee created successful");
	    } catch (Exception e) {  
	        tx.rollback();  
	        e.printStackTrace();  
	    }finally{  
	        session.close();  
	    }  
	}
	
	public void delete(Employee persistentInstance) {
		log.debug("deleting Employee instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Employee findById(java.lang.Integer id) {
		log.debug("getting Employee instance with id: " + id);
		try {
			Employee instance = (Employee) getSession().get(
					"com.esms.bean.Employee", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Employee instance) {
		log.debug("finding Employee instance by example");
		try {
			List results = getSession()
					.createCriteria("com.esms.bean.Employee").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Employee instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Employee as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByPersonid(Object personid) {
		return findByProperty(PERSONID, personid);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByProgress(Object progress) {
		return findByProperty(PROGRESS, progress);
	}

	public List findAll() {
		log.debug("finding all Employee instances");
		try {
			String queryString = "from Employee";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
//	public List findAllEmployeeByEnterprise(String ent) {
//		log.debug("finding all Employee by enterprise name");
//		try {
//			String queryString =  "from enterprise where name=?";
//			Query queryObject = getSession().createQuery(queryString);
//			queryObject.setParameter(0, ent);
//			return queryObject.list();
//		} catch (RuntimeException re) {
//			log.error("find all failed", re);
//			throw re;
//		}
//	}

	public Employee merge(Employee detachedInstance) {
		log.debug("merging Employee instance");
		try {
			Employee result = (Employee) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Employee instance) {
		log.debug("attaching dirty Employee instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Employee instance) {
		log.debug("attaching clean Employee instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}